<?php

namespace app\goods\controller;

use cmf\controller\AdminBaseController;
use app\goods\model\GoodsTypeModel;
use think\Db;

class AdminGoodsTypeController extends AdminBaseController
{

    public function _initialize() {
        parent::_initialize();
        $this->goodsTypeModel = new GoodsTypeModel();
    }

    /**
     * 产品类型列表
     * @adminMenu(
     *     'name'   => '产品类型',
     *     'parent' => 'goods/AdminGoodsType/index',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '产品类型列表',
     *     'param'  => ''
     * )
     */
    public function index(){
        $data        = $this->goodsTypeModel->getTypeList();

        $this->assign('list', $data->items());
        $this->assign('page', $data->render());

        return $this->fetch();
    }

    /**
     * 添加产品类型
     * @adminMenu(
     *     'name'   => '添加产品类型',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加产品类型',
     *     'param'  => ''
     * )
     */
    public function add(){

        return $this->fetch();
    }

    /**
     * 添加产品类型提交
     * @adminMenu(
     *     'name'   => '添加产品类型提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加产品类型提交',
     *     'param'  => ''
     * )
     */
    public function addPost(){
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'AdminGoodsType');
            if ($result !== true) {
                $this->error($result);
            }

            $data['site_id'] = get_site_id();

            $res = db('goods_type')->insertGetId($data);

            if( $res ){

                //操作日志
                admin_log('', 'add', 'goods_type');

                $this->success('添加成功!', url('AdminGoodsType/index'));
            }else{
                $this->error("添加失败！");
            }
        }
    }

    /**
     * 编辑产品类型
     * @adminMenu(
     *     'name'   => '编辑产品类型',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑产品类型',
     *     'param'  => ''
     * )
     */
    public function edit(){
        $id   = $this->request->param('id',0,'intval');
        $data = $this->goodsTypeModel->find($id);

        $this->assign("data",$data);

        return $this->fetch();
    }

    /**
     * 编辑产品类型提交
     * @adminMenu(
     *     'name'   => '编辑产品类型提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑产品类型提交',
     *     'param'  => ''
     * )
     */
    public function editPost(){
        $data   = $this->request->param();
        $result = $this->validate($data, 'AdminGoodsType');
        if ($result !== true) {
            $this->error($result);
        }

        $res = $this->goodsTypeModel->update($data);

        if( $res ){

            //操作日志
            admin_log('', 'edit', 'goods_type');

            $this->success('修改成功！');
        }else{
            $this->error("修改失败！");
        }
    }

    /**
     * 文章删除
     * @adminMenu(
     *     'name'   => '文章删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章删除',
     *     'param'  => ''
     * )
     */
    // 删除产品类型
    public function delete() {
        $id = $this->request->param('id', 0, 'intval');

        if ( db('goods_type')->delete($id)!==false ) {
            /* 清除该类型下的所有属性 */
            $arr = db('goods_attribute')->where('cat_id', $id)->column('attr_id');
            if( !empty($arr) ){
                $where = array(
                    'attr_id' => array('IN',$arr),
                );
                db('goods_attribute')->where($where)->delete();
                db('goods_attr')->where($where)->delete();
            }

            //操作日志
            admin_log('', 'delete', 'goods_type');

            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

}
